<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from '../../views/dashboard/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '250px'
    },
    autoResize: {
      type: Boolean,
      default: true
    },
    chartData: {
      type: Object,
      required: true
    },
    lineLegendData:{
      type: Array,
      default: []
    }
  },
  data() {
    return {
      chart: null
    }
  },
  watch: {
  },
  mounted() {
    this.$nextTick(() => {
      console.log('chartData',this.chartData);
      this.initChart()
    })
  },
  beforeDestroy() {
    // if (!this.chart) {
    //   return
    // }
    // this.chart.dispose()
    // this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions(this.chartData)
    },
    setOptions() {
      this.chart.setOption({
        xAxis: {
          type: 'category',
          data: this.chartData.months,
          // boundaryGap: false,
          axisTick: {
            // show: false
          },
          axisLabel: {
          },
          axisLine: {
              lineStyle: {
                  color:  '#666666',
                  width: 1,
              }
          },
        },
         grid: {
          left: '3%',
          right: '4%',
          bottom: '3%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'cross'
          },
          padding: [5, 10]
        },
        yAxis: {
          axisLine: {
              lineStyle: {
                color:  '#666666',
                width: 1,
              },
          },
        },
        legend: {
          data: this.lineLegendData,
          x: 'right', // 水平安放位置，默认为全图居中，可选： 'center' ¦ 'left' ¦ 'right'  或 {number}（x坐标，单位px）
          y: 'top',  //垂直安放位置，默认为全图顶端，可选： 'top' ¦ 'bottom' ¦ 'center' 或 {number}（y坐标，单位px）
          icon: "circle",
        },

        series: [
          {
                    name: this.lineLegendData[0],
                    type: 'line', //图表的类型，line就是折线图，pie就是饼图等等
                    smooth: true, //变成平滑的效果
                    lineStyle: {
                        color: '#51ABFF',
                        type: 'solid' //虚线的效果 dotted 点线  solid 实线(默认)
                    },
                    label: {
                        // show: true, //显示数值
                        position: 'top' //位置标签
                    },
                    itemStyle: {
                        color: '#51ABFF'
                    },
                    data: this.chartData.boys,
                    // areaStyle:{}
                },
                {
                    name: this.lineLegendData[1],
                    type: 'line',
                    label: {
                        // show: true, //显示数值
                        position: 'top' //位置标签
                    },
                    itemStyle: {
                        color: '#FF6363'
                    },
                    lineStyle: {
                        color: '#FF6363',
                        type: 'solid' //虚线的效果 dotted 点线  solid 实线(默认)
                    },
                    data:this.chartData.girls
                }
        ],

      })
    }
  }
}
</script>
